package com.jihe.ehmall.product.controller;

import com.jihe.common.utils.PageUtils;
import com.jihe.common.utils.R;
import com.jihe.ehmall.product.entity.AttrEntity;
import com.jihe.ehmall.product.entity.AttrGroupEntity;
import com.jihe.ehmall.product.service.AttrAttrgroupRelationService;
import com.jihe.ehmall.product.service.AttrGroupService;
import com.jihe.ehmall.product.service.AttrService;
import com.jihe.ehmall.product.service.CategoryService;
import com.jihe.ehmall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * ???Է??
 *
 * @author zyw
 * @email zyw@gmail.com
 * @date 2021-09-07 14:34:33
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R listNoAttrRelation(@RequestParam Map<String, Object> params ,@PathVariable("attrGroupId") Long attrGroupId){
        PageUtils page = attrService.queryPageNoAttrRelation(params , attrGroupId);
        return R.ok().put("page", page);
    }
    /**
     * 查询未关联的属性
     */


    /**
     * 关联属性列表
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R listAttrRelation(@PathVariable("attrGroupId") Long attrGroupId){
        List<AttrEntity> page = attrService.queryPageAttrRelation(attrGroupId);

        return R.ok().put("data", page);
    }

    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> relationVoList){
        attrAttrgroupRelationService.saveBatchs(relationVoList);
        return R.ok();
    }
    /**
     * 列表
     */
    @RequestMapping("/list/{categoryId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params ,
                  @PathVariable("categoryId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params , catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] catelogPath = categoryService.findCatelogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(catelogPath);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 关联属性列表
     */
    @RequestMapping("/attr/relation/delete")
    public R deleteAttrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrAttrgroupRelationService.removeAttrRelation(attrGroupRelationVos);
        return R.ok();
    }

}
